<template>
	<view class="mainpadding">
		<view class="flexbetween pdl" @click="checkTime(1)">
			<view class="xiaohei main_size" style="min-width: 200rpx;">请假开始时间</view>
			<view class="flexright">
				<input type="text" class="textright xiaohei main_size" placeholder="请选择请假开始时间" disabled=""
					v-model="start_time" />
				<u-icon name="arrow-right" color="#333" size="12"></u-icon>
			</view>
		</view>
		<view class="flexbetween margin_top pdl" @click="checkTime(2)">
			<view class="xiaohei main_size" style="min-width: 200rpx;">请假结束时间</view>
			<view class="flexright">
				<input type="text" class="textright xiaohei main_size" placeholder="请选择请假结束时间" disabled=""
					v-model="end_time" />
				<u-icon name="arrow-right" color="#333" size="12"></u-icon>
			</view>
		</view>
		<view class="margin_top pdl">
			<view class="xiaohei main_size">请假原因</view>
			<textarea class="margin_top xiaohei main_size" style="width: 100%;height: 150rpx;" placeholder="请输入请假原因"
				v-model="reason" /></textarea>
		</view>
		<view class="xiaohei margin_top main_size">其他信息</view>
		<view class="flexleft flex_wrap">
			<view class="flexcenter margin_top" style="width: 25%;" v-for="(item,index) in upImg_text" :key="index">
				<view class="tupian dingwei">
					<image :src="item" mode=""></image>
					<view class="shanchu" @click="deleimgae(index)"></view>
				</view>
			</view>
			<view class="flexcenter margin_top" style="width: 25%;">
				<view class="tupian" @click="uploadImg" v-if="upImg_text.length>=4?false:true">
					<image src="@/static/upimg.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="mainpadding_top gudingdb">
			<view class="submitbtn" @click="submit">确认提交</view>
		</view>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm" @timecancel="timecancel"></timePicker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				upImg: [], //图片路径
				upImg_text: [], //图片路径
				messageTime: {
					mode: "datetime",
					timeShow: false,
					title: "请选择请假开始时间",
					minDate: Number(new Date())
				},
				start_time: "",
				end_time: "",
				reason: ""
			}
		},
		methods: {
			submit() {
				if (this.start_time == "") {
					httpRequest.toast("请选择请假开始时间")
					return false
				}
				if (this.end_time == "") {
					httpRequest.toast("请选择请假结束时间")
					return false
				}
				if (Number(new Date(this.start_time)) > Number(new Date(this.end_time))) {
					httpRequest.toast("开始时间不能大于结束时间")
					return false
				}
				if (this.reason == "") {
					httpRequest.toast("请输入请假原因")
					return false
				}
				httpRequest.request('/api/Companydk/addCompanyQj', 'GET', {
					start_time: this.start_time,
					end_time: this.end_time,
					reason: this.reason,
					images: this.upImg.join()
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			checkTime(val) {
				if (val == 1) {
					this.messageTime.title = "请选择请假开始时间"
					this.messageTime.timeShow = true
				} else {
					this.messageTime.title = "请选择请假结束时间"
					this.messageTime.timeShow = true
				}
			},
			timeConfirm(e) {
				if (this.messageTime.title == "请选择请假开始时间") {
					this.start_time = e
				} else {
					this.end_time = e
				}
			},
			timecancel() {
				this.messageTime.timeShow = false
			},
			// 删除照片
			deleimgae(index) {
				this.upImg.splice(index, 1)
				this.upImg_text.splice(index, 1)
			},
			// 上传多张照片
			uploadImg() {
				let _this = this;
				let upImgs = _this.upImg;
				let num = 4 - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {

						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], _this.upImg, _this.upImg_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {

									_this.uploadImageService(res[i], _this.upImg, _this.upImg_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			// 详情转载
			uploadImageService(url, upImg, upImg_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					let image = res.url;
					upImg.push(image);
					upImg_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style scoped lang="scss">
	.tupian {
		width: 147rpx;
		height: 147rpx;
		border-radius: 14rpx;

		image {
			width: 147rpx;
			height: 147rpx;
			border-radius: 14rpx;
		}
	}

	.shanchu {
		width: 32rpx;
		height: 32rpx;
		position: absolute;
		top: 0;
		right: 0;
		background-size: 100% 100%;
		background-image: url('@/static/shanchu1.png');
	}
</style>